package cn.bjsxt.plane;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import cn.bjsxt.util.Constant;
import cn.bjsxt.util.GameUtil;

public class Plane extends GameObject {
	private boolean left, up, right, down;
	private boolean live = true;
	
	public void draw(Graphics g) {
		if (live) {
			g.drawImage(img, (int)x, (int)y, null);
			move();
		}
		
	}
	
	public Plane(String imagePath, double x, double y) {
		super();
		this.img = GameUtil.getImage(imagePath);
		this.x = x;
		this.y = y;
		this.width = img.getWidth(null);
		this.height = img.getHeight(null);
	}
	
	public Plane() {
	}
	
	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	public void move() {
		if (left) {
			x -= speed;
			if (x < 0) {
				x = 0;
			}
		}
		if (right) {
			x += speed;
			if (x > Constant.GAME_WIDTH - 29) {
				x = Constant.GAME_WIDTH - 29;
			}
		}
		if (up) {
			y += speed;
			if (y > Constant.GAME_HEIGHT - 43) {
				y = Constant.GAME_HEIGHT - 43;
			}
		}
		if (down) {
			y -= speed;
			if (y < 20) {
				y = 20;
			}
		}
	}
	
	public void addDirection(KeyEvent e, boolean isTrue) {
		switch(e.getKeyCode()) {
			case KeyEvent.VK_LEFT:
				left = isTrue;
				break;
			case KeyEvent.VK_UP:
				down = isTrue;
				break;
			case KeyEvent.VK_RIGHT:
				right = isTrue;
				break;
			case KeyEvent.VK_DOWN:
				up = isTrue;
				break;
			default:
				break;
		}
	}
}


